﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;
using Jungle.Code.Utils;
using Jungle.Code.Core;

using Jungle.Code.CategoryManager;
using Jungle.Code.ProductManager;
using System.Net;

namespace Jungle.Code.CategoryManager.WebControls
{
    public class CategoriesListUserPart: WebControl
    {

        protected override void RenderContents(HtmlTextWriter writer)
        {
            
            #region CategoriesListTable
            /*

            

            string linkStr = "";
            string catId = "";

            if (Context.Request["catId"] != null)
            {
                catId = Context.Request["catId"];
            }
            if (linkStr.IndexOf("/ru") != -1)
            {
                linkStr = Context.Request.RawUrl;
                linkStr = linkStr.Substring(0, linkStr.IndexOf("/ru"));
            }
            else
            {
                linkStr = Jungle.Code.Utils.UrlHelper.GetSiteUrl();
            }

            JungleTreeMenu AllCats = new JungleTreeMenu();
            

            if (catId != "")
            {
                AllCats.GetMainItems();
                foreach (JungleTreeMenu singleCat in AllCats.MenuItemsList)
                {
                    if (singleCat.MenuVisible && singleCat.ID > 1)
                    {
                        if (catId == singleCat.ID.ToString())
                        {                            
                            JungleTreeMenu subCats = new JungleTreeMenu();
                            subCats.ID = singleCat.ID;
                            subCats.GetById();
                            subCats.GetChildItems();
                            int i = 0;

                            writer.AddAttribute("class", "menu_items_div");
                            writer.RenderBeginTag(HtmlTextWriterTag.Div);                            
                            writer.AddAttribute("class", "menu_items");
                            writer.AddAttribute(HtmlTextWriterAttribute.Href, linkStr + "/ru/main.aspx?cmd=view&catId=" + singleCat.ID.ToString().Trim());
                            writer.RenderBeginTag(HtmlTextWriterTag.A);
                            writer.Write(singleCat.MenuText.Trim());
                            writer.RenderEndTag();
                            writer.RenderEndTag();
                            
                            

                            foreach (JungleTreeMenu subItems in subCats.MenuItemsList)
                            {
                                i++;
                                if (i > 1)
                                {
                                    writer.AddAttribute("class", "menu_subitems_div");
                                    writer.RenderBeginTag(HtmlTextWriterTag.Div);
                                    writer.AddAttribute("class", "menu_subitems_container_div");
                                    writer.RenderBeginTag(HtmlTextWriterTag.Div);
                                    writer.AddAttribute("class", "menu_subitems");
                                    writer.AddAttribute(HtmlTextWriterAttribute.Href, linkStr + "/ru/main.aspx?cmd=view&catId=" + subItems.ID.ToString().Trim());
                                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                                    writer.Write(subItems.MenuText.Trim());
                                    writer.RenderEndTag();
                                    writer.RenderEndTag();
                                    writer.RenderEndTag();
                                }

                            }

                        }
                        else
                        {
                            writer.AddAttribute("class", "menu_items_div");
                            writer.RenderBeginTag(HtmlTextWriterTag.Div);
                            writer.AddAttribute("class", "menu_items");
                            writer.AddAttribute(HtmlTextWriterAttribute.Href, linkStr + "/ru/main.aspx?cmd=view&catId=" + singleCat.ID.ToString().Trim());
                            writer.RenderBeginTag(HtmlTextWriterTag.A);
                            writer.Write(singleCat.MenuText.Trim());
                            writer.RenderEndTag();
                            writer.RenderEndTag();
                        }
                    }
                }
            }
            else
            {
                AllCats.GetMainItems();
                foreach (JungleTreeMenu singleCat in AllCats.MenuItemsList)
                {
                    if (singleCat.MenuVisible && singleCat.ID > 1)
                    {
                        writer.AddAttribute("class", "menu_items_div");
                        writer.RenderBeginTag(HtmlTextWriterTag.Div);
                        writer.AddAttribute("class", "menu_items");
                        writer.AddAttribute(HtmlTextWriterAttribute.Href, linkStr + "/ru/main.aspx?cmd=view&catId=" + singleCat.ID.ToString().Trim());
                        writer.RenderBeginTag(HtmlTextWriterTag.A);
                        writer.Write(singleCat.MenuText.Trim());
                        writer.RenderEndTag();
                        writer.RenderEndTag();
                    }
                }
            }
            

            */
            
            #endregion
            
            
            #region TEST
           
            JungleTreeMenu jMenu = new JungleTreeMenu();
            JungleTreeMenu jSubItems = new JungleTreeMenu();

            jMenu.GetAll();
            int counter = 0;
            
            foreach (JungleTreeMenu jMainItems in jMenu.MenuItemsList)
            {
                if (jMainItems.LevelId == 2)
                {
                     
                    writer.AddAttribute("class", "menu_items_div");
                    writer.RenderBeginTag(HtmlTextWriterTag.Div);
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, "/ru/main.aspx?cmd=view&catId=" + jMainItems.ID.ToString());
                    if (jMainItems.GetChildItems() > 1)
                    {
                        counter++;
                        writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "javascript:onClickNode('" + counter + "');");
                    }
                    writer.AddAttribute(HtmlTextWriterAttribute.Class, "menu_items");
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                    writer.Write(jMainItems.MenuText.Trim());
                    writer.RenderEndTag();
                    writer.RenderEndTag();
                    
                    
                    
                    jSubItems.ID = jMainItems.ID;
                    jSubItems.LeftKey = jMainItems.LeftKey;
                    jSubItems.RightKey = jMainItems.RightKey;

                    if (jSubItems.GetChildItems() > 1)
                    {
                        writer.AddAttribute("id", "menu" + counter.ToString());
                        writer.AddAttribute("class", "menu_span");
                        writer.RenderBeginTag(HtmlTextWriterTag.Span);

                        foreach (JungleTreeMenu jChild in jSubItems.MenuItemsList)
                        {
                            if (jChild.LevelId > 2 && jChild.ID != jSubItems.ID && jChild.MenuVisible != false)
                            {                                
                                writer.AddAttribute("class", "menu_subitems_div");
                                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                                if (jChild.LevelId <= 5)
                                {
                                    writer.AddAttribute("class", "menu_subitem_level_" + jChild.LevelId.ToString());
                                }
                                else 
                                {
                                    writer.AddAttribute("class", "menu_subitem_level_5");
                                }
                                writer.RenderBeginTag(HtmlTextWriterTag.Div);
                                writer.AddAttribute(HtmlTextWriterAttribute.Href, UrlHelper.GetSiteUrl() + "/ru/main.aspx?cmd=view&catId=" + jChild.ID.ToString());
                                writer.AddAttribute("class", "menu_subitems");
                                writer.RenderBeginTag(HtmlTextWriterTag.A);
                                writer.Write(jChild.MenuText.Trim());
                                writer.RenderEndTag();
                                writer.RenderEndTag();
                                writer.RenderEndTag();
                            }
                        }
                        writer.RenderEndTag();

                    }

                }                 
            }

            
            HttpCookie menuCookie = new HttpCookie("structure_tree");          
            
            if (Context.Request.Cookies.Get("structure_tree") == null)
            {
                string cookieDefault = "";
                for (int i = 1; i <= counter; i++) { cookieDefault += "0"; }                
                menuCookie.Value = cookieDefault;
                menuCookie.Path = "/ru/";
                Context.Response.Cookies.Add(menuCookie);
            }
            /*
             function JS(menu) {
            nameDiv = eval("document.getElementById(menu)");
            if (nameDiv.style.display == 'none') {
                nameDiv.style.display = 'block';
            }
            else
            { nameDiv.style.display = 'none'; }
        }
             * 
            function onClickNode(id) {
            var nodeDiv = document.getElementById("menu" + id);            
            var flag = 0;
            if (nodeDiv.style.display == 'none') {	// скрыть ветку
            
                nameDiv.style.display = 'block';
                // обновить cookie переменную 
                var structure_tree = _get_var("structure_tree");
                structure_tree = structure_tree.substr(0, id-1) + flag + structure_tree.substr(id);
                _set_var("structure_tree", structure_tree);
                               
            }
            else {	// показать ветку
                flag = 1;                
                // обновить cookie переменную 
                var structure_tree = _get_var("structure_tree");
                structure_tree = structure_tree.substr(0, id-1) + flag + structure_tree.substr(id);
                _set_var("structure_tree", structure_tree);
                nameDiv.style.display = 'none';
            }
        }

            */

            #endregion
            
            base.RenderContents(writer);
        }
        
      
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintBtnDel(HtmlTextWriter writer, int catId)
        {
            ClientScriptManager cs = Page.ClientScript;
            PostBackOptions opt = new PostBackOptions(this);
            opt.ActionUrl = Context.Request.RawUrl;
            opt.Argument = "delCategory," + catId.ToString();

            writer.AddAttribute(HtmlTextWriterAttribute.Onclick, "if(confirm(\"Вы действительно хотите удалить данную категорию? Все дочерние категории будут не доступны для просмотра в меню. \"))" + cs.GetPostBackEventReference(opt));
            writer.AddAttribute("title", "Удалить");
            writer.AddAttribute("style", "cursor: pointer;");
            PrintImage(writer, "/Admin/img/btn_blue_delete.png");
            return writer;
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintBtnEdit(HtmlTextWriter writer, int catId)
        {
            string linkStr = Context.Request.RawUrl;
            int t = linkStr.IndexOf("cmd");

            if (t != -1)
            {
                linkStr = linkStr.Substring(0, t + 4);
            }

            writer.AddAttribute(HtmlTextWriterAttribute.Href, linkStr + "editCategory&id=" + catId.ToString());
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.AddAttribute("title", "Редактировать");
            writer.AddAttribute("style", "cursor: pointer;");
            PrintImage(writer, "/Admin/img/btn_blue_edit.png");
            writer.RenderEndTag();
            return writer;
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintText(HtmlTextWriter writer, string Text, int cID)
        {
            string link = Context.Request.RawUrl;
            if (Context.Request.RawUrl.IndexOf("&catId=") != -1)
            {
                link = link.Substring(0, link.IndexOf("&catId="));
            }
            writer.AddAttribute(HtmlTextWriterAttribute.Href, link + "&catId=" + cID.ToString());
            writer.AddAttribute("style", "cursor: pointer;");
            writer.RenderBeginTag(HtmlTextWriterTag.A);
            writer.Write(Text + "!");
            writer.RenderEndTag();
            return writer;
        }
        //-----------------------------------------------------------------------------------------------
        public HtmlTextWriter PrintImage(HtmlTextWriter writer, string imgUrl)
        {
            writer.AddAttribute(HtmlTextWriterAttribute.Src, UrlHelper.GetSiteUrl() + imgUrl);
            writer.AddAttribute(HtmlTextWriterAttribute.Align, "absmiddle");
            writer.RenderBeginTag(HtmlTextWriterTag.Img);
            writer.RenderEndTag();
            return writer;
        }
        //-----------------------------------------------------------------------------------------------
        
    }
}
